package zhtt;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by zhtt on 2017/4/10.
 */
public class StudentJunitTest_parameter {

    private Session session;

    @Before
    public void setUp(){
        session=HbnUtil.getSession();
    }

    @Test/** SQL查询 order by score DESC**/
    public void sql(){
        try{
            session.beginTransaction();
            String sql="select * from zhtt0_student order by score DESC ";//用表名
            List<Student> studentList=session.createSQLQuery(sql).addEntity(Student.class).list();
            for(Student student:studentList){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test /** HQL查询  Hibernate query language **/
    public void hql(){
        try{
           session.beginTransaction();
            String hql="from Student order by score DESC";//用类名
            List<Student> studentList=session.createQuery(hql).list();
            for(Student student:studentList){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){

        }
    }

    @Test /** Query By Criteria **/
    public void qbc(){
        try{
            session.beginTransaction();
            List<Student> studentList=session.createCriteria(Student.class)
                    .addOrder(Order.desc("score")).list();//属性名
            for(Student student:studentList){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){

        }
    }

    @Test /** Query By Criteria **/
    public void qbc1(){
        try{
            session.beginTransaction();
            session.getTransaction().commit();
        }catch (Exception e){

        }
    }

}
